package ControlLayer;
import ModelLayer.*;
import java.util.Date;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.lang.Object;


public class OrderCtr
{
    private Order o;
    private Orders orders;
    private Products products;
    private Items items;
    private Customers customers;
    private PaymentTypes paymentTypes;
    private DeliveryTypes deliveryTypes;
    private Discounts discounts;
    private Staffs staffs;
    private XMLDecoder xDecode;
   
    public OrderCtr()
    {
       discounts.getInstance();
       orders.getInstance();
       products.getInstance();
       items.getInstance();
       customers.getInstance();
       paymentTypes.getInstance();
       deliveryTypes.getInstance();
       staffs.getInstance();
       o = new Order();
    }

    public void createOrder(String _id, String _productId, String _itemId, String _customerId, String _deliveryTypeId, String _paymentTypeId, String _staffId)
    {
         o.setId(_id);
         Date sysDate = new Date();
         String dateString = sysDate.toString();
         o.setDateCreated(dateString);
         o.addProduct(products.getProduct(_productId));
         o.setCustomer(customers.getCustomer(_customerId));
         o.setPaymentType(paymentTypes.getPaymentType(_paymentTypeId));
         if(_deliveryTypeId != "")
            o.setDeliveryType(deliveryTypes.getDeliveryType(_deliveryTypeId));
         o.setStaff(staffs.getStaff(_staffId));
         if(_itemId != "")
            o.addItem(items.getItem(_itemId));
         o.setStatus("Created");
         orders.addOrder(o);
    }
    
    public void addProduct(String _productId)
    {
        o.addProduct(products.getProduct(_productId));
    }
    
    public void deleteProduct(String _productId)
    {
        o.removeProduct(_productId);
    }
    
    public void addItem(String _ItemId)
    {
        o.addItem(items.getItem(_ItemId));
    }
    
    public void deleteItem(String _itemId)
    {
        o.removeItem(_itemId);
    }
    
    public double calculateSubtotal()
    {
        o.calculateSubtotal();
        return o.getSubtotal();
    }
    
    public double calculateTotal()
    {
        o.calculateTotal();
        return o.getTotalPrice();
    }
    
    public void deleteOrder(String _orderId)
    {
        orders.getOrder(_orderId).setStatus("Deleted");
    }
    
    public void listAllOrders()
    {
        System.out.println("\nOrders:");
        for(int i = 0; i<orders.getSize(); i++)
        {
            System.out.println("\nID: " + orders.allOrders(i).getId());
            System.out.println("\nTotal Price: " + orders.allOrders(i).getTotalPrice() + "\n");
        }
    }
    
    public void listAllProducts()
    {
        products.printProducts();
    }
    
    public void listAllItems()
    {
        items.listAllItems();
    }
    
    public void listAllCustomers()
    {
        customers.printCustomers();
    }
    
    public void listAllPaymentTypes()
    {
        paymentTypes.printPaymentTypes();
    }
    
    public void listAllDeliveryTypes()
    {
        deliveryTypes.printDeliveryTypes();
    }
    
    public Order findOrder(String _orderId)
    {
        return orders.getOrder(_orderId);
    }
    
    public void listAllStaffs()
    {
        staffs.listAllStaffs();
    }
    
    public void loadData()
    {
        /*FileInputStream fis = null;
        for(int i=0;i<10;i++)
        {
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\discount" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            discounts.addDiscount(Discount.cast(o));
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\deliveryType" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            deliveryTypes.addDeliveryType(o);
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\paymentType" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            paymentType.addPaymentType(o);
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\products" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            products.addProduct(o);
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\item" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            items.addItem(o);
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\customer" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            customers.addCustomer(o);
            xDecode.close();
            
            try{
                fis = new FileInputStream("C:\\Users\\Vigidis\\Dropbox\\Workspace\\BlueJ Workspace\\final_project\\semester-project-group1\\data\\staff" + i + ".xml");
            }
            catch(Exception e){}
            xDecode = new XMLDecoder(new BufferedInputStream(fis));
            Object o = xDecode.readObject();
            staffs.addStaff(o);
            xDecode.close();
        }*/
    }
}
